import dayjs from 'dayjs'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import useConfigStore from '../../store/config'
import { getTimeZone } from './intl'

dayjs.extend(utc)
dayjs.extend(timezone)

interface IFormatTimeParams {
  timestamp: number | string
  timezone?: string
  format?: string
}

export const timezoneName = getTimeZone()

export function formatTime({
  timestamp,
  timezone,
  format = 'YYYY-MM-DD HH:mm:ss',
}: IFormatTimeParams) {
  const configStore = useConfigStore()
  return dayjs(timestamp).tz(timezone || configStore.timezoneName).format(format)
}

export function stringToTimestamp(
  dateString: string,
  timezone?: string,
) {
  const configStore = useConfigStore()
  return dayjs.tz(dateString, timezone || configStore.timezoneName).valueOf()
}
